package com.neusoft.util;

import com.neusoft.annotation.ID;
import com.neusoft.annotation.Table;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MySqlProvider {
    public static final String INSERT = "insert";
    public static final String DELETE = "delete";
    public static final String DELETE1 = "delete1";
    public static final String UPDATE = "update";

    public static String insert(Object obj) throws IllegalAccessException {
        Map<String,String> map = new HashMap<>();
        String table = getTableName(obj);
        getMap(obj,map);
        System.out.println(obj);
        System.out.println(table);
        return new SQL() {
            {
                INSERT_INTO(table);
                for (String key : map.keySet()) {
                    VALUES(key, map.get(key));
                }
            }
        }.toString();
    }

    public static String delete(String table, String where) {
        if (StringUtils.isEmpty(table)) {
            return null;
        }
        if (StringUtils.isEmpty(where)) {
            return null;
        }
        return new SQL() {
            {
                DELETE_FROM(table);
                WHERE(where);
            }
        }.toString();
    }

    public static String delete1(String table, String where) {
        System.out.println("table为："+table);
        if (StringUtils.isEmpty(table)) {
            return null;
        }
        if (StringUtils.isEmpty(where)) {
            return null;
        }
        return new SQL() {
            {
                DELETE_FROM(table);
                WHERE(where);
            }
        }.toString();
    }

    public static String update(Object obj) throws IllegalAccessException {
        Map<String, String> map = new HashMap<>();
        String table = getTableName(obj);
        System.out.println("表名为："+table);
        String idName = getMap(obj,map);
        if(StringUtils.isEmpty(idName)){
            throw new RuntimeException("实体类->"+obj.getClass().getCanonicalName()+"必须有@ID注解");
        }
        String idValue = map.get(idName);
        map.remove(idName);
        return new SQL() {
            {
                UPDATE(table);
                for (String key : map.keySet()) {
                    SET(key+"="+map.get(key));
                }
                WHERE(idName+"="+idValue);
            }
        }.toString();
    }

    public static String getTableName(Object obj) {
        Class c = obj.getClass();
        Table table = (Table) c.getAnnotation(Table.class);
        if (table != null) {
            return table.value();
        }
        return StringUtil.underscoreName(c.getSimpleName());
    }

    public static String getIdName(Field obj) {
        Class c = obj.getClass();
        ID id = (ID) c.getAnnotation(ID.class);
        if(id != null && !StringUtils.isEmpty(id.value())){
            return id.value();
        }
        return StringUtil.underscoreName(obj.getName());
    }

    /**
     * 将对象中非空属性放到map中，并且返回对象带有@ID注解的值或属性名
     * @param obj
     * @param map
     * @return
     * @throws IllegalAccessException
     */
    public static String getMap(Object obj,Map<String,String> map) throws IllegalAccessException {
        Class c = obj.getClass();
        Field[] fs = c.getDeclaredFields();//获取所有属性
        String idName = null;
        for (Field item : fs) {
            String key = item.getName();
            item.setAccessible(true);
            if(idName == null){
                idName = getIdName(item);
            }
            if (item.get(obj) != null) {
                map.put(StringUtil.underscoreName(key), "#{" + key + "}");
            }

        }
        return idName;
    }
}
